<template>
  <div class="ratings"
       ref="ratings">
    <scroller>
      <div class="over_view">
        <div class="over_left">
          <h1 class="score">{{seller.score}}</h1>
          <p class="title">综合评分</p>
          <p class="rank">高于周边商家{{seller.rankRate}}%</p>
        </div>
        <div class="over_right">
          <div class="score_wrapper">
            <span class="title">服务态度</span>
            <star :score="seller.serviceScore"
                  :size="36"></star>
            <span class="score">{{seller.serviceScore}}</span>
          </div>
          <div class="score_wrapper">
            <span class="title">商品评分</span>
            <star :score="seller.foodScore"
                  :size="36"></star>
            <span class="score">{{seller.foodScore}}</span>
          </div>
          <div class="delivery_wrapper">
            <span class="title">送达时间</span>
            <span class="delivery">{{seller.deliveryTime}}</span>
          </div>
        </div>
      </div>
      <div class="split"></div>
      <div class="rating_select">
        <div class="rating_type">
          <span class="selectButton"
                :class="{raringSelect:-1 == raringSelect}"
                @click="raringSelectClick(-1)">全部
            <span class="count">{{ratings.length}}</span>
          </span>
          <span class="selectButton"
                :class="{raringSelect:0 == raringSelect}"
                @click="raringSelectClick(0)">满意
            <span class="count">{{positiveLength}}</span>
          </span>
          <span class="selectButton"
                :class="{raringSelect:1 == raringSelect}"
                @click="raringSelectClick(1)">不满意
            <span class="count">{{ratings.length-positiveLength}}</span>
          </span>
        </div>
        <div class="switch"
             :class="{switchOn:switchOn}"
             @click="switchClick">
          <span class="icon-check_circle"></span>
          <span class="text">只看有内容的评价</span>
        </div>
      </div>
      <div class="rating_wrapper">
        <ul>
          <li v-for="(item,index) in ratings"
              :key="index">
            <div class="rating_item"
                 v-if="!(switchOn && item.text.length == 0)
           && (raringSelect==-1||item.rateType == raringSelect)">
              <div class="avatar">
                <img class="img"
                     width="28"
                     height="28"
                     :src="item.avatar">
              </div>
              <div class="content">
                <h1 class="name">{{item.username}}</h1>
                <div class="star_wrapper">
                  <star :score="item.score"
                        :size="24"></star>
                  <span class="delivery">{{item.deliveryTime}}</span>
                </div>
                <p class="text">{{item.text}}</p>
                <div class="recmmend">
                  <span :class="item.rateType==0?`icon-thumb_up`:`icon-thumb_down`"></span>
                  <span class="item"
                        v-for="(recommend,index) in item.recommend"
                        :key="index">{{recommend}}</span>
                </div>
                <div class="time">{{item.rateTime | date-string}}</div>
              </div>
            </div>
          </li>
        </ul>
      </div>
    </scroller>
  </div>
</template>
<script>
import axios from 'axios'
import star from '../star/star.vue'
import BScroll from 'better-scroll'
export default {
  props: ['seller'],
  components: {
    star
  },
  data () {
    return {
      ratings: [],
      raringSelect: -1,
      switchOn: false,
      ratingScro: null
    }
  },
  created () {
    axios.get('/api/ratings').then(response => {
      const result = response.data
      if (result.code === 0) {
        this.ratings = result.data
        // Vue.nextTick(() => {
        //   this.initScroller()
        // })
      }
    })
  },
  methods: {
    initScroller () {
      this.ratingScro = new BScroll(this.$refs.ratings, {
        click: true
      })
    },
    raringSelectClick (type) {
      this.raringSelect = type
      this.refreshScroller()
    },
    switchClick () {
      this.switchOn = !this.switchOn
      this.refreshScroller()
    },
    refreshScroller () {
      // 切换了评价分类栏。下一帧刷新滚动框
      // Vue.nextTick(() => {
      //   this.ratingScro.refresh()
      // })
    }
  },
  computed: {
    positiveLength () {
      return this.ratings.reduce((preTotal, rating) => {
        return preTotal + (rating.rateType === 0 ? 1 : 0)
      }, 0)
    }
  }

}
</script>
<style lang="stylus" rel="stylesheet/stylus">
@import '../../common/stylus/mixins.styl';

.ratings {
  position: absolute;
  width: 100%;
  top: 190px;
  bottom: 0;
  overflow: hidden;

  .over_view {
    display: flex;
    padding: 18px 0;

    .over_left {
      flex: 0 0 140px;
      padding: 6px 0;
      text-align: center;
      border-right: 1px solid rgba(7, 17, 27, 0.1);

      .score {
        line-height: 28px;
        font-size: 24px;
        margin-bottom: 6px;
        color: #f90;
      }

      .title {
        line-height: 12px;
        font-size: 12px;
        margin-bottom: 10px;
        color: #07111b;
      }

      .rank {
        line-height: 10px;
        font-size: 10px;
        color: #93999f;
      }
    }

    .over_right {
      padding: 6px 0 6px 24px;

      .score_wrapper {
        padding-bottom: 8px;

        .star {
          display: inline-block;
          vertical-align: top;
          margin: 0 12px;
        }

        .title, .score {
          vertical-align: top;
          line-height: 18px;
          font-size: 12px;
          color: #07111b;
        }

        .score {
          color: #f90;
        }
      }

      .delivery_wrapper {
        .title {
          line-height: 18px;
          font-size: 12px;
          color: #07111b;
        }

        .delivery {
          margin-left: 12px;
          line-height: 12px;
          font-size: 12px;
          color: #93999f;
        }
      }
    }
  }

  .split {
    height: 16px;
    border-top: 1px solid rgba(7, 17, 27, 0.1);
    border-bottom: 1px solid rgba(7, 17, 27, 0.1);
    background-color: #f3f5f7;
  }

  .rating_select {
    .rating_type {
      padding: 18px;
      border-bottom: 1px solid rgba(7, 17, 27, 0.1);

      .selectButton {
        padding: 8px 12px;
        margin-right: 8px;
        line-height: 16px;
        font-size: 12px;
        background-color: rgba(0, 160, 220, 0.2);

        .count {
          margin-left: 2px;
          font-size: 8px;
        }
      }

      .raringSelect {
        background-color: rgb(0, 160, 220);
      }
    }

    .switch {
      padding: 12px 18px;
      line-height: 24px;
      border-bottom: 1px solid rgba(7, 17, 27, 0.1);
      color: rgb(147, 153, 159);

      .text {
        font-size: 12px;
        vertical-align: top;
      }

      .icon-check_circle {
        vertical-align: top;
        margin-right: 4px;
        font-size: 24px;
      }

      &.switchOn {
        .icon-check_circle {
          color: #22c850;
        }
      }
    }
  }

  .rating_wrapper {
    padding: 0 18px;

    .rating_item {
      display: flex;
      padding: 18px 0;
      border_1px(rgba(7, 17, 27, 0.1));

      .avatar {
        flex: 0 0 28px;
        width: 28px;
        margin-right: 12px;

        .img {
          border-radius: 50%;
        }
      }

      .content {
        position: relative;
        flex: 1;

        .name {
          margin-bottom: 4px;
          line-height: 12px;
          font-size: 10px;
          color: rgb(7, 17, 27);
        }

        .star_wrapper {
          margin-bottom: 4px;
          font-size: 0;

          .star {
            display: inline-block;
            vertical-align: top;
            margin-right: 6px;
          }

          .delivery {
            display: inline-block;
            vertical-align: top;
            line-height: 12px;
            font-size: 10px;
            color: #93999f;
          }
        }

        .text {
          line-height: 18px;
          font-size: 12px;
          margin-bottom: 8px;
          color: #07111b;
        }

        .recmmend {
          line-height: 16px;
          font-size: 0;

          .icon-thumb_up, .icon-thumb_down, .item {
            display: inline-block;
            margin: 0 8px 4px 0;
            font-size: 9px;
          }

          .icon-thumb_up {
            color: #00a0dc;
          }

          .icon-thumb_down {
            color: #93999f;
          }

          .item {
            padding: 0 6px;
            border: 1px solid rgba(7, 17, 27, 0.1);
            border-radius: 1px;
            color: rgb(147, 153, 159);
            background: #fff;
          }
        }

        .time {
          position: absolute;
          right: 0;
          top: 0;
          line-height: 12px;
          font-size: 10px;
          color: #93999f;
        }
      }
    }
  }
}
</style>
